﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fortuitus.Core
{
    /// <summary>
    /// OrderedList is a generic ordered list that implements IOrderedIEnumerable by preserving the order of elements.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class OrderedList<T> :  IOrderedList<T>
    {
        
        //TODO: should be an array since List  doesn't guarantee to preserve the order of items, works in .Net and SL but dont' know about Mono or future .Net versions
        private List<T> _list = new List<T>();


        public OrderedList(IEnumerable<T> list=null)
        {
            if (list != null)
                _list.AddRange(list);
        }

        public T this[int indx]
        {
            get { return _list[indx]; }
        }

        public void Add(T item)
        {
            _list.Add(item);
        }


        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }


        public int Length
        {
            get { return _list.Count; }
        }
    }
}
